package com.ibeetl.cms.service;

import com.ibeetl.admin.core.dao.CoreDictDao;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.service.BaseService;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.cms.dao.SincerityDao;
import com.ibeetl.cms.entity.Sincerity;
import org.apache.commons.collections.ListUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 诚信管理 Service
 */

@Service
@Transactional
public class SincerityService extends BaseService<Sincerity>{

    @Autowired
    private SincerityDao sincerityDao;

    @Autowired
    private CoreDictDao coreDictDao;

    //个人诚信
    public PageQuery<Sincerity> queryByCondition(PageQuery query){
        return businessTypeAction(query,0);
    }

    //单位诚信
    public PageQuery<Sincerity> enterpriselist(PageQuery query){
        return businessTypeAction(query,1);
    }
    /**
     * 业务类型从哦的转中文统一处理
     * @param query
     * @param userType
     */
    private PageQuery<Sincerity> businessTypeAction(PageQuery query,int userType) {
        PageQuery ret = new PageQuery();
        if(0 == userType) {
            //个人用户
            ret = sincerityDao.queryByCondition(query);
        }else if(1 == userType){
            //企业用户
            ret = sincerityDao.enterpriselist(query);
        }else{
            return null;
        }
        List<CoreDict> coreDictList = coreDictDao.findAllList("buinessType");
        Map coreDictMap = new HashMap<String,String>(16);
        for (CoreDict coreDict:coreDictList) {
            coreDictMap.put(coreDict.getValue(),coreDict.getName());
        }
        List<Sincerity> sincerityList = ret.getList();
        for(Sincerity sincerity : sincerityList){
            String bussinessTypeZn = "";
            String buinessType = sincerity.getBusinessType();
            String [] buinessTypeArray = buinessType.split(",");
            for (String temp:buinessTypeArray){
                bussinessTypeZn += ","+coreDictMap.get(temp);
            }
            sincerity.setBusinessType(bussinessTypeZn.substring(1));
        }
        queryListAfter(sincerityList);
        return ret;
    }

    //判断诚信用于初审
    public List<Sincerity> blacklist(Integer typeId,Integer companyId,String businessType){
        List<Sincerity> sincerityList = sincerityDao.blacklist(typeId,companyId,businessType);
        List<Sincerity> sincerityListRet = new ArrayList<Sincerity>(10);
        if(null == sincerityList || sincerityList.size() == 0){
            return new ArrayList<Sincerity>();
        }
        for(Sincerity sincerity:sincerityList){
            String businessTypeStr = sincerity.getBusinessType();
            String [] businessTypeArry = businessTypeStr.split(",");
            for(String s: businessTypeArry){
                if(s.equals(businessType)) {
                    //如果黑名单记录中存在该业务项 则将该条记录返回
                    sincerityListRet.add(sincerity);
                }
            }
        }
        return sincerityListRet;
    }

    public void batchDelSincerity(List<Long> ids){
        try {
            sincerityDao.batchDelSincerityByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除诚信管理失败", e);
        }
    }
}